package cn.zhangxin.pethome.system.pet.service.impl;

import cn.zhangxin.pethome.constants.BaseConstants;
import cn.zhangxin.pethome.jwt.LoginContextUtils;
import cn.zhangxin.pethome.system.employee.domain.Employee;
import cn.zhangxin.pethome.system.pet.domain.Pet;
import cn.zhangxin.pethome.system.pet.domain.SearchMasterMsg;
import cn.zhangxin.pethome.system.pet.domain.SearchMasterMsgAuditLog;
import cn.zhangxin.pethome.system.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.zhangxin.pethome.system.pet.mapper.SearchMasterMsgMapper;
import cn.zhangxin.pethome.system.pet.service.ISearchMasterMsgService;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import cn.zhangxin.pethome.system.shop.domain.Shop;
import cn.zhangxin.pethome.system.shop.mapper.ShopMapper;
import cn.zhangxin.pethome.system.user.domain.User;
import cn.zhangxin.pethome.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-02-08
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private AuthService authService;
    @Autowired
    private SearchMasterMsgAuditLogMapper auditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        User loginUser = (User)LoginContextUtils.getLoginUser(request);
        searchMasterMsg.setUserId(loginUser.getId());
        if(!authService.authentication(searchMasterMsg.getTitle())){
            throw new BusinessException("标题存在敏感字符!");
        }
        if(!authService.authentication(searchMasterMsg.getName())){
            throw new BusinessException("名字存在敏感字符!");
        }
        searchMasterMsg.setState(BaseConstants.Publish.STATE_WAIT_ACTIVE);
        searchMasterMsg.setNote("审核通过！");
        //用户传入的经纬度
        Point point = BaiduMapUtils.getPoint(searchMasterMsg.getAddress());
        List<Shop> all = shopMapper.findAll();
        List<Shop> collect = all.stream()
                .filter(address -> {
                    //数据库查询出来的经纬度
                    Point point1 = BaiduMapUtils.getPoint(address.getAddress());
                    return BaiduMapUtils.getDistance(point, point1) < 50000;
                }).collect(Collectors.toList());
        if (collect.isEmpty()){
            //放入宠物池
        }else {
            Double min = 50000.0;
            Long shopId = null;
            for (Shop shop : collect) {
                Point point1 = BaiduMapUtils.getPoint(shop.getAddress());
                double distance = BaiduMapUtils.getDistance(point, point1);
                if (distance<min){
                    min = distance;
                    shopId = shop.getId();
                }
            }
            searchMasterMsg.setShopId(shopId);
        }
        searchMasterMsgMapper.add(searchMasterMsg);

        //补充日志表
        SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
        searchMasterMsgAuditLog.setMsgId(searchMasterMsg.getId());
        searchMasterMsgAuditLog.setState(BaseConstants.Publish.STATE_SUCCESS);
        searchMasterMsgAuditLog.setAuditTime(new Date());
        searchMasterMsgAuditLog.setNote("审核成功！");
        auditLogMapper.add(searchMasterMsgAuditLog);
    }
    //通过用户id查询用户的宠物
    @Override
    public AjaxResult myCenter(Long id) {
        List<SearchMasterMsg> searchMasterMsg = searchMasterMsgMapper.myCenter(id);
        return AjaxResult.success().setData(searchMasterMsg);
    }

    @Override
    public List<SearchMasterMsg> toSearchMasterPool() {
        return searchMasterMsgMapper.toSearchMasterPool();
    }

    //接单成功后的逻辑
    @Override
    public void orderTaking(HttpServletRequest request) {
        Employee employee = (Employee)LoginContextUtils.getLoginUser(request);
        if (employee==null){
            throw new BusinessException("非法访问！！！");
        }
        //12小时后设置清空店铺id并且设置状态
    }
}
